package com.kedll.library.fragment;

import android.content.Context;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;

import com.kedll.library.R;
import com.kedll.library.mvp.AbstractRefreshPresenter;
import com.kedll.library.mvp.RefreshingView;
import com.kedll.library.util.Log;

/**
 * 所有下拉刷新的基类
 * Created by QiQi on 16/1/12.
 */
public abstract class KRefreshFragment<V extends ViewDataBinding, T extends AbstractRefreshPresenter> extends KBaseFragment<V, T> implements OnRefreshListener, RefreshingView {
    protected SwipeRefreshLayout refreshLayout;

    protected int page = 1;

    @Override
    public void onRefresh() {
        page = 1;
        mPresenter.refreshData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (refreshLayout != null) {
            refreshLayout.setOnRefreshListener(null);
        }
    }

    @Override
    protected void init() {
        Log.i("attach", "------attach View-------");
        super.init();
        mPresenter.onAttach(this);
        refreshLayout = mBinding.getRoot().findViewById(R.id.refreshLayout);
        if (refreshLayout != null) {
            refreshLayout.setColorSchemeResources(R.color.orangeRed, R.color.green);
        }
    }

    @Override
    protected void initEvent(Bundle savedInstanceState) {
        if (refreshLayout != null) {
            refreshLayout.setOnRefreshListener(this);
        }
    }

    @Override
    public void setRefreshing(boolean refreshing) {
        if (refreshLayout != null) {
            refreshLayout.setRefreshing(refreshing);
        }
    }

    @Nullable
    @Override
    public Context getContext() {
        return super.getContext();
    }
}
